package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
)

func process(conn net.Conn) {

	defer conn.Close()
	for {
		var tmp [128]byte

		n, err := conn.Read(tmp[:])
		if err != nil {
			fmt.Println("read err", err)
			break
		}

		fmt.Println("read from client messages: ", string(tmp[:n]))

		fmt.Println("请回复：")
		buff := bufio.NewScanner(os.Stdin)
		buff.Scan()
		reply := buff.Text()
		msg := "server:" + reply
		conn.Write([]byte(msg))

	}
}

func main() {
	listener, err := net.Listen("tcp", "127.0.0.1:6000")
	if err != nil {
		fmt.Println("listen 127.0.0.1:6000 failed:", err)
		os.Exit(2)
	}
	for {
		con, err := listener.Accept()
		if err != nil {
			fmt.Println("accept err ", err)
			return
		}
		go process(con)
	}

}
